<?php
$params = array();

$SEARCH = new search();


if (!empty($_GET['reindex']))
{
   $was_interupted = FALSE;
   $search_params  = config::read('search');

   if(file_exists(ROOT . CACHE .'/search.interupt'))
   {
      $was_interupted = unserialize(file_get_contents(ROOT . CACHE .'/search.interupt'));
      $was_interupted = $FILTER->getFirstInteger($was_interupted);
   }

   $langs = $REQUEST->getLangAllId();

   $sql  = 'SELECT `t1`.`id`, `cat`, `lang`, `sdate`, `title`, `description`, `body`
      FROM `meccano_articles` AS `t1`, `meccano_articles_translate` AS `t2`
      WHERE `t1`.`id` = `t2`.`id`
      AND `lang` IN (' . implode(', ', $langs) . ')';
   if(!empty($search_params['category']))
   {
      $cats = array();
      foreach($search_params['category'] AS $c)
      {
         $c = $TREE->getChildrenId($c);
         if(!empty($c)) $cats = array_merge($cats, $c);
      }
      $sql .= ' AND `t1`.`cat` IN ('. join(',', $cats) .') ';
   }
   if($was_interupted) $sql .= ' AND `t1`.`id` >= '. $was_interupted;
   $sql .= ' AND `deleted` IS NULL ORDER BY `id`, `lang`';

   $DB->sql = $sql;
   $DB->DB_Fetch();

   $indexed = 0;
   $start_timer = time();
   $interupted = FALSE;

   for ($i = 0, $t = $DB->rows; $i < $t; $i++)
   {
      $article = array();
      $l = $DB->fetched[$i]['lang'];
      $article[$l]['id'] = $DB->fetched[$i]['id'];
      $article[$l]['cat'] = $DB->fetched[$i]['cat'];
      $article[$l]['lang'] = $l;
      $article[$l]['sdate'] = $DB->fetched[$i]['sdate'];
      $article[$l]['title'] = $DB->fetched[$i]['title'];
      $article[$l]['description'] = $DB->fetched[$i]['description'];
      $article[$l]['body'] = $DB->fetched[$i]['body'];

      $SEARCH->addArticle($article);

      if ((time() - $start_timer) > 40)
      {
         $interupted = $DB->fetched[$i]['id'];
         file_put_contents(ROOT . CACHE . '/search.interupt', serialize($interupted));
         break;
      }

      set_time_limit(30);
   }

   $SMARTY->assign('reindexed', $SEARCH->getIndexCount());
   $SMARTY->assign('interupted', $interupted);
   /**
    * Block if execution time limit
    */

   if(!$interupted && file_exists(ROOT . CACHE . '/search.interupt'))
   {
      unlink(ROOT . CACHE . '/search.interupt');
   }
   set_time_limit(90);
   $SEARCH->optimizeIndex();
}



if (!empty($_POST))
{
   if (!empty($_POST['f_title']))
      $params['title'] = true;

   if (!empty($_POST['f_description']))
      $params['description'] = true;

   if (!empty($_POST['f_body']))
      $params['body'] = true;

   if (!empty($_POST['f_category']))
   {
      if (is_array($_POST['f_category_params']))
      {
         $params['category'] = array();
         foreach ($_POST['f_category_params'] AS $v)
         {
            if ($v == 'all' || $TREE->getNode($v))
            {
               $params['category'][] = $v;
            }
         }
         if (empty($params['category']))
            $params['category'] = FALSE;
      } else
      {
         $params['category'] = FALSE;
      }
   }


   if (!empty($params))
      config::write('search', 'search', $params);
}


$params = config::read('search');

# PREPARE PARAMS VALUE
if (!empty($params))
{
   //var_dump($params); exit();
   //$params = unserialize($params);
   # PREPARE PARAMS VALUE FOR CATEGORY CRITERIA
   if (!empty($params['category']))
   {
      $tmp = array();
      for ($i = 0, $t = count($params['category']); $i < $t; $i++)
      {
         if ($params['category'][$i] == 'all')
         {
            $tmp['all'] = gettext::translate('meccano', 'allcategories');
         } else
         {
            $node = $TREE->getNode($params['category'][$i]);
            if ($node)
            {
               $tmp[$node->id] = $node->title;
            }
         }
      }

      if (empty($tmp))
         $tmp['all'] = gettext::translate('meccano', 'allcategories');
      $params['category'] = $tmp;
   }
}


$SMARTY->assign('data', $params);
$layout = 'search.html';